﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Common.Lib
{
    public class UserAuthenticate
    {
        private DBConnector dbConnector;

        string sSql_GetCtrls = "SELECT ELEMENT_ID,ACTIVE_FLAG,PARENT_ELEMENT" +
                               " FROM PERMISSION P," +
                                    "(SELECT SCREEN_ID,PERMISSION" +
                                     " FROM USER_ROLE UR, USERS U" +
                                     " WHERE UR.MS_NHOM_QLY = U.MS_NHOM_QLY" +
                                            " AND USERNAME LIKE @username AND SCREEN_ID = @scrID) T" +
                                " WHERE P.SCREEN_ID = T.SCREEN_ID" +
                                      " AND P.PERMISSION LIKE T.PERMISSION";

        public UserAuthenticate()
        {
            dbConnector = new DBConnector();
        }

        public void setRights(Form fParentForm, string sUserName)
        {
            if (dbConnector.connect())
            {
                dbConnector.addParameters("@username", sUserName);
                dbConnector.addParameters("@scrID", fParentForm.Name);

                DataTable dbRes = dbConnector.selectData(sSql_GetCtrls);
                dbRes.PrimaryKey = new DataColumn[] { dbRes.Columns["ELEMENT_ID"] };
                bool bEnable = false;

                try
                {
                    foreach (Control c in fParentForm.Controls)
                    {
                        if (c.GetType().Equals(typeof(GroupBox)))
                        {
                            GroupBox gb = (GroupBox)c;
                            foreach (Control cc in gb.Controls)
                            {
                                DataRow row = dbRes.Rows.Find(cc.Name);
                                if (row != null)
                                {
                                    bEnable = Util.parseBoolean(row["ACTIVE_FLAG"]);
                                    setProperty(cc, bEnable);
                                }
                            }
                        }
                        else
                        {
                            DataRow row = dbRes.Rows.Find(c.Name);
                            if (row != null)
                            {
                                bEnable = Util.parseBoolean(row["ACTIVE_FLAG"]);
                                setProperty(c, bEnable);
                            }
                        }
                    }
                    DataRow rows = dbRes.Rows.Find("dateTimePicker_ToDate");
                    //foreach (DataRow row in dbRes.Rows)
                    //{
                    //    string parentID = Util.parseString(row["PARENT_ELEMENT"]);
                    //    Control c = null;
                    //    bEnable = Util.parseBoolean(fParentForm.Controls["ACTIVE_FLAG"]);
                    //    if (string.IsNullOrEmpty(parentID))
                    //    {
                    //        ctrlID = Util.parseString(fParentForm.Controls["ELEMENT_ID"]);
                    //        setProperty(c, bEnable);
                    //    }
                    //    else
                    //    {
                    //        if (fParentForm.Controls["PARENT_ELEMENT"].GetType().Equals(typeof(GroupBox)))
                    //        {
                    //            GroupBox gb = (GroupBox)c;
                    //            Control cc = gb.Controls[Util.parseString(fParentForm.Controls["ELEMENT_ID"])];
                    //            setProperty(cc, bEnable);
                    //        }
                    //    }
                    //}
                }
                catch (Exception ex)
                {
                    Logger.addLog(Logger.ERROR + "\n" + this.ToString() + " : =================================\n" + ex.Message);
                }
                dbConnector.removeParameters();
                dbConnector.close();
            }
        }

        private void setProperty(Control c, bool bValue)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)c).ReadOnly = !bValue;
            }
            else
            {
                c.Enabled = bValue;
            }
        }
    }
}
